package com.tarena.lbs.user.dao.repository.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.user.dao.mapper.UserGroupMapper;
import com.tarena.lbs.user.dao.repository.UserGroupRepository;
import com.tarena.lbs.user.pojo.po.AdminPO;
import com.tarena.lbs.user.pojo.po.UserGroupPO;
import com.tarena.lbs.user.pojo.query.AdminQuery;
import com.tarena.lbs.user.pojo.query.UserGroupQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Slf4j
public class UserGroupRepositoryImpl implements UserGroupRepository {

    @Autowired
    private UserGroupMapper userGroupMapper;
    @Override
    public PageInfo<UserGroupPO> pageGroupList(UserGroupQuery userGroupQuery) {
        log.info("查询参数:{}",userGroupQuery);
        QueryWrapper<UserGroupPO> queryWrapper = assembleUserGroupQuery(userGroupQuery);
        //PageHelper.startPage 之间不能有代码
        PageHelper.startPage(userGroupQuery.getPageNo(), userGroupQuery.getPageSize());
        List<UserGroupPO> pos = userGroupMapper.selectList(queryWrapper);
        //selectList 数据库操作 之间不能有代码
        return new PageInfo<>(pos);
    }
    private QueryWrapper<UserGroupPO> assembleUserGroupQuery(UserGroupQuery query) {
        QueryWrapper<UserGroupPO> queryWrapper = new QueryWrapper<>();
        //1.groupName 人群名 模糊查询 非空 where group_name like(%#{}%)
        if (StringUtils.isNotBlank(query.getGroupName())){
            queryWrapper.like("group_name",query.getGroupName());
        }
        //2.releaseStatus 发布状态 相等查询 非空 where release_status=#{releaseStatus}
        if (query.getReleaseStatus() != null){
            queryWrapper.eq("release_status",query.getReleaseStatus());
        }
        //3.startTime 开始时间 大于等于查询 非空 where create_at>=#{startTime}
        if (query.getStartTime() != null){
            queryWrapper.ge("create_at",query.getStartTime());
        }
        //4.endTime 结束时间 小于等于查询 非空 where create_at <= #{endTime}
        if (query.getEndTime() != null){
            queryWrapper.le("create_at",query.getEndTime());
        }
        return queryWrapper;
    }
    @Override
    public Long selectByGroupName(String groupName) {
        QueryWrapper<UserGroupPO> queryWrapper = new QueryWrapper();
        queryWrapper.eq("group_name", groupName);
        return userGroupMapper.selectCount(queryWrapper);
    }

    @Override
    public void insertGroup(UserGroupPO po) {

        log.info("创建群组:{}",po);
        userGroupMapper.insert(po);
    }

    @Override
    public void deleteGroup(Integer groupId) {
        log.info("删除群组:{}",groupId);
        userGroupMapper.deleteById(groupId);
    }

    @Override
    public UserGroupPO selectGroupById(Integer groupId) {
        return userGroupMapper.selectById(groupId);
    }


}

